﻿#region 命名空间

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL.PageFactory
{
    [ToolboxData("<{0}:CategoryShow runat=\"server\"></{0}:CategoryShow>")]
    public class CategoryShow : DataBox
    {
        #region 父栏目id

        private int? _parentId;

        public int? ParentId
        {
            set { this._parentId = value; }
            get
            {
                if (this._parentId == null)
                {
                    if (Page.RouteData.Values.ContainsKey("CategoryId"))
                        this._parentId = Convert.ToInt32(Page.RouteData.Values["CategoryId"]);
                    else
                    {
                        this._parentId = Convert.ToInt32(Page.RouteData.Values["ID"]);
                    }
                }
                return this._parentId;
            }
        }

        #endregion

        #region 级别

        public int Level { get; set; }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            if (!ParentId.HasValue)
            {
                return;
            }
            IEnumerable<Category> list;
            if (Level > 0)
            {
                Category info = CategoryManage.GetInfo(ParentId.Value);
                string[] ids = info.Path.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
                if (ids.Length < Level - 1)
                {
                    return;
                }
                list = CategoryManage.GetChildren(int.Parse(ids[Level - 2]));
                DataSource = list;
            }
            else
            {
                list = CategoryManage.GetChildren(ParentId.Value);
                DataSource = list;
            }
            DataBind();
        }
    }
}